<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    public function login(){
        //显示登录页面
        return view('home.login');
    }

    //验证登录
    public function checkLogin(Request $request){
        //接收表单传来的信息
        $uname=$request->input('uname');
        $upwd=md5($request->input('upwd'));

        //查询用户信息
        $result=DB::table('users')
            ->select('uid','uname','uemail')
            ->where('uname','=',$uname)
            ->where('upwd','=',$upwd)
            ->first();
        //验证用户名密码是否匹配
        if($result){
            //查询到
            //写入session
            $request->session()->put('isHome',true);//登陆标志
            $request->session()->put('uid',$result['uid']);
            $request->session()->put('uname',$result['uname']);
            $request->session()->put('uemail',$result['uemail']);
            //重定向  redirect
            return redirect('/home/index');
        }else{
            //查询不到   滚回登陆
            return redirect('/home/login');
        }
    }

    //退出登录
    public function logout(Request $request){
        //清楚session信息
        $request->session()->flush();
        //跳转页面
        return redirect('/home/login');
    }
}
